package com.example.take_out_dish.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_dish.common.Result;
import com.example.take_out_dish.common.lang.dto.CategoryDto;
import com.example.take_out_dish.common.lang.vo.BaseVo;
import com.example.take_out_dish.entity.Category;
import com.example.take_out_dish.entity.Dish;
import com.example.take_out_dish.exceptions.BusinessException;
import com.example.take_out_dish.exceptions.enumetation.ResponseEnum;
import com.example.take_out_dish.service.CategoryService;
import com.example.take_out_dish.service.DishService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private DishService dishService;

    @PostMapping("page")
    public Result<Page> page(@RequestBody BaseVo baseVo) {
        log.info("/category/page baseVo:{}", baseVo);
        Page pageInfo = categoryService.page(new Page(baseVo.getPage(), baseVo.getPageSize()));
        log.info("pageInfo:{}", pageInfo);
        return Result.success(pageInfo);
    }

    @PostMapping
    public Result save(@RequestBody CategoryDto categoryDto) {
        log.info("新增分类: {}", categoryDto);
        categoryService.saveCategory(categoryDto);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") String id){
        log.info("删除分类：{}", id);
        categoryService.deleteCategoryById(id);
        return Result.success();
    }


    @PutMapping
    public Result<Object> update(@RequestBody CategoryDto categoryDto) {
        categoryService.updateCategoryById(categoryDto);
        return Result.success();
    }

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}/{id}")
    public Result startOrStop(@PathVariable("status") Integer status,
                              @PathVariable("id") String id){
        categoryService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

}
